package com.sobercoding.loopauthdemo;

import com.sobercoding.loopauth.abac.AbacStrategy;
import com.sobercoding.loopauth.abac.model.AbacPoAndSu;
import com.sobercoding.loopauth.abac.model.builder.AbacPolicyFunBuilder;
import com.sobercoding.loopauth.exception.LoopAuthExceptionEnum;
import com.sobercoding.loopauth.exception.LoopAuthPermissionException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Sober
 */
@Configuration
public class Configss {

    @Bean
    public void policyFun() {
        AbacStrategy.abacPoAndSuMap = new AbacPolicyFunBuilder()
                // 自定义登录id校验的鉴权规则
                .setPolicyFun("loginId1",
                        // 创建规则校验及获取当前值的方式
                        new AbacPoAndSu()
                                // 创建校验方式  value为当前值即setSupplierMap提供的值
                                // rule为规则的值即 Policy setProperty 的值
                                .setMaFunction((value, rule) -> {
                                    // 当前用户id需要与规则匹配才可访问  否则 抛出异常
                                    if (!value.equals(rule)){
                                        throw new LoopAuthPermissionException(LoopAuthExceptionEnum.NO_PERMISSION);
                                    }
                                })
                                // 获得value方式
                                .setSupplierMap(() -> "1")
                ).build();
    }

}
